# coding=utf-8

from app.execute_cmd import execute_cmds, bakupFiles, checkFilesBalance, filesIndb, get_SFiles_From_ZWCL, start_Xfer, \
    check_Xfer, stop_Xfer, check_SFiles, SocreCalcFilter, SocreCalcStartCalc, filesIndbCheck, WriteOffStartCalc
from app.main import main
from flask import request, jsonify, current_app
from config import config
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


@main.route('/execute', methods=['POST', 'GET'])
def execute():
    user_agent = request.headers.get('User-Agent')  # 客户端系统相关信息
    current_app.logger.info('客户端系统信息: %s', user_agent)
    user_ip = request.remote_addr  # 客户端的IP地址
    current_app.logger.info('客户端IP地址: %s', user_ip)
    name = request.form.get('name')  # 客户端请求命令
    current_app.logger.info('客户端请求命令: %s', name)

    command = str(name)
    result = execute_cmds(command)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/get_SFiles_from_zw', methods=['POST', 'GET'])
def get_SFiles_from_zw():
    commands = config['default'].GET_S_FILES
    result = get_SFiles_From_ZWCL(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/start_xfer', methods=['POST', 'GET'])
def start_xfer():
    commands = config['default'].GET_S_FILES
    result = start_Xfer(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/count_files', methods=['POST', 'GET'])
def count_files():
    commands = config['default'].GET_S_FILES
    result = check_Xfer(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/check_files', methods=['POST', 'GET'])
def check_files():
    commands = config['default'].GET_S_FILES
    result = check_SFiles(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/stop_xfer', methods=['POST', 'GET'])
def stop_xfer():
    commands = config['default'].GET_S_FILES
    result = stop_Xfer(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/s_files_bakup', methods=['POST', 'GET'])
def s_files_bakup():
    commands = config['default'].S_FILES_BAKUP
    result = bakupFiles(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/check_files_balance', methods=['POST', 'GET'])
def check_Files_Balance():
    commands = config['default'].CHECK_FILES_BALANCE
    result = checkFilesBalance(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/files_indb', methods=['POST', 'GET'])
def files_Indb():
    commands = config['default'].FILES_INDB
    result = filesIndb(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/files_indb_check', methods=['POST', 'GET'])
def files_Indb_Check():
    commands = config['default'].FILES_INDB_CHECK
    result = filesIndbCheck(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/SocreCalc_filter', methods=['POST', 'GET'])
def SocreCalc_filter():
    commands = config['default'].SCORECALC_FILTER
    current_app.logger.info('执行命令：%s', commands)
    result = SocreCalcFilter(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/SocreCalc_StartCalc', methods=['POST', 'GET'])
def SocreCalc_StartCalc():
    commands = config['default'].SCORECALC_STARTCALC
    current_app.logger.info('执行命令：%s', commands)
    result = SocreCalcStartCalc(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})


@main.route('/WriteOff_StartCalc', methods=['POST', 'GET'])
def WriteOff_StartCalc():
    commands = config['default'].WRITEOFF_LOG_CHECK
    current_app.logger.info('执行命令：%s', commands)
    result = WriteOffStartCalc(commands)
    # current_app.logger.info('命令执行结果：%s', result)
    return jsonify({'result': result})
